/***
 * Irc class : simple implementation of a chat using JAVANAISE
 * Contact: 
 *
 * Authors: 
 */

package irc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Serializable;

import jvn.client.JvnException;
import jvn.client.JvnObjectImpl;
import jvn.client.JvnServerImpl;
import jvn.client.interfaces.JvnLocalServerI;
import jvn.client.interfaces.JvnObjectI;

public class IrcCLI {

	private static String ObjectName;
	Jvn_Sentence sentence;

	/**
	 * main method create a JVN object nammed IRC for representing the Chat application
	 **/
	public static void main(String argv[]) {
		try {

			System.out.println("Entrer le nom de l'objet partagé : ");

			try (BufferedReader entree = new BufferedReader(new InputStreamReader(System.in))) {

				String st = entree.readLine();

				Jvn_Sentence.jvnRename(st);

				Jvn_Sentence sentence = new Jvn_Sentence();

				new IrcCLI(sentence, entree);
			}
		} catch (Exception e) {
			System.out.println("IRC problem : " + e.getMessage());
			e.printStackTrace();
		}

	}

	/**
	 * IRC Constructor
	 * 
	 * @param sentence2
	 *            the JVN object representing the Chat
	 * @param entree2
	 *            the input stream
	 **/
	public IrcCLI(Jvn_Sentence sentence2, BufferedReader entree) {
		sentence = sentence2;

		String st = "";
		boolean cont = true;
		while (cont) {
			System.out.print(sentence.jvnGetLockState() + "$>");

			try {
				st = entree.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}

			String[] cars = st.split("\\s");
			if (cars.length == 0)
				continue;

			try {
				switch (cars[0]) {
				case "h":
					System.out.println("Aide : " //
							+ "\n l 	: Lire" //
							+ "\n e text : Ecrire" //
							+ "\n al true/false : défini l'autolock" //
							+ "\n r 	: Lock Read " //
							+ "\n w 	: Lock Write"//
							+ "\n u 	: Unlock"//
							+ "\n t 	: Begin Transaction" //
							+ "\n rb 	: Roll Back" //
							+ "\n c 	: Commit");
					break;
				case "l":
					System.out.println(sentence.read());
					break;
				case "al":
					if (cars.length >= 2 && cars[1] != null) {
						if ('t' == cars[1].charAt(0)) {
							sentence.jvnSetAutolock(true);
						} else {
							sentence.jvnSetAutolock(false);
						}
					} else {
						System.out.println("Penser à écrire un argument : true ou false.");
					}
					break;
				case "e":
					if (cars.length >= 2 && cars[1] != null) {
						String s = "";
						for (int i = 1; i < cars.length; i++) {
							s += cars[i] + " ";
						}
						sentence.write(s);
					} else {
						System.out.println("Penser à écrire le texte à enregistrer.");
					}
					break;
				case "r":
					sentence.jvnLockRead();
					break;
				case "w":
					sentence.jvnLockWrite();
					break;
				case "u":
					sentence.jvnUnLock();
					break;
				case "t":
					sentence.jvnBeginTransaction();
					break;
				case "rb":
					sentence.jvnRollBack();
					break;
				case "c":
					sentence.jvnCommit();
					break;
				default:
					if (cars.length >= 1 && !"".equals(cars[0])) {
						System.out.println("Commande inconnue");
					}
					break;
				}
			} catch (JvnException e) {
				System.out.println("JvnException : " + e.getMessage());
			}

		}

	}
}
